package com.cncbox.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cncbox.entity.NoteResult;
import com.cncbox.entity.User;
import com.cncbox.service.UserManagerService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Develop_Peng
 * @version V1.0
 * @Title: UserManagerController.java
 * @Package com.cncbox.controller
 * @Description: 用户登陆校验(类描述)
 * @date 2016年3月28日 上午11:17:12
 */

@Controller
public class UserManagerController {

    @Autowired
    private UserManagerService userManagerService;

    /**
     * 用户登陆校验
     *
     * @param user 传入用户名和密码
     * @return 返回相关json数据
     * @throws Exception
     */
    @RequestMapping(value = "/signin")
    public String checkUser(HttpSession httpSession, HttpServletRequest httpServletRequest, User user) throws Exception {
        //返回
        NoteResult result = new NoteResult();
        String userName = user.getUserName().trim();
        String password = user.getPassword().trim();
        if ("".equals(userName) || "".equals(password)) {
            result.setStatus(0);
            result.setMsg("PARAMETER_ERROR");
        } else {
            result = userManagerService.checkUserNamePassword(user);
            Integer fin = (Integer) result.getData();
            if (0 < fin) {
                httpSession.setAttribute("userName", userName);
            }

        }
        httpServletRequest.setAttribute("message", result);
        return "redirect:/manager/index.html";
    }

    /**
     * 修改用户密码
     *
     * @param password 传入密码
     * @return 跳转页面
     * @throws Exception
     */
    @RequestMapping(value = "/manager/updatePass")
    public String updateUserPassword(HttpSession httpSession, HttpServletRequest httpServletRequest, String password) throws Exception {
        NoteResult result = new NoteResult();
        if (password != null && !"".equals(password)) {
            result = userManagerService.updateUserPassword(password);
            httpSession.invalidate();
        } else {
            result.setStatus(0);
            result.setMsg("PARAMETER_ERROR");
        }
        httpServletRequest.setAttribute("message", result);
        return "redirect:/manager/jump.html";
    }

    /**
     * 退出登录
     *
     * @param httpSession
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/manager/logout")
    public String logout(HttpSession httpSession) throws Exception {
        String userName = (String) httpSession.getAttribute("userName");
        if (userName != null && !"".equals(userName)) {
            httpSession.invalidate();
        }
        return "redirect:/manager/signin.html";
    }

}
